<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\logic\Advertisement as CommonAdvertisement;

/**
 * 广告管理逻辑
 */
class Advertisement extends AdminBase
{
    
    public static $commonAdvertisementLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonAdvertisementLogic) && static::$commonAdvertisementLogic = new CommonAdvertisement();
    }
    
    /**
     * 获取广告列表搜索条件
     * @param array $data 搜索参数
     * @return array
     */
    public function getWhere($data = [])
    {
        return static::$commonAdvertisementLogic->getWhere($data);
    }

    /**
     * 获取广告列表（后台专用）
     */
    public function getAdvertisementList($where = [], $field = true, $order = 'sort DESC, id DESC', $paginate = DB_LIST_ROWS)
    {
        return static::$commonAdvertisementLogic->getAdvertisementList($where, $field, $order, $paginate);
    }
    
    /**
     * 获取广告信息
     */
    public function getAdvertisementInfo($where = [], $field = true)
    {
        return static::$commonAdvertisementLogic->getAdvertisementInfo($where, $field);
    }
    
    /**
     * 添加/编辑广告
     */
    public function advertisementEdit($data = [])
    {
        $url = url('advertisementList');
        
        // 验证必填字段
        if (empty($data['title'])) {
            return [RESULT_ERROR, '广告标题不能为空'];
        }
        
        if (empty($data['position'])) {
            return [RESULT_ERROR, '广告位置不能为空'];
        }
        
        // 验证位置是否有效
        $positionOptions = $this->modelAdvertisement->getPositionOptions();
        if (!isset($positionOptions[$data['position']])) {
            return [RESULT_ERROR, '广告位置无效'];
        }
        
        // 设置默认值
        $data['status'] = $data['status'] ?? $this->modelAdvertisement::STATUS_ENABLED;
        $data['sort'] = $data['sort'] ?? 0;
        
        if (!empty($data['id'])) {
            // 编辑广告
            $result = static::$commonAdvertisementLogic->editAdvertisement($data);
            $action = '编辑';
        } else {
            // 添加广告
            $result = static::$commonAdvertisementLogic->addAdvertisement($data);
            $action = '新增';
        }
        
        if ($result[0] == RESULT_SUCCESS) {
            action_log($action, $action . '广告，标题：' . $data['title']);
            return [RESULT_SUCCESS, $result[1], $url];
        } else {
            return [RESULT_ERROR, $result[1]];
        }
    }
    
    /**
     * 删除广告
     */
    public function advertisementDel($data = [])
    {
        $url = url('advertisementList');
        
        if (empty($data['id'])) {
            return [RESULT_ERROR, '参数错误'];
        }
        
        $result = static::$commonAdvertisementLogic->deleteAdvertisement($data);
        
        if ($result[0] == RESULT_SUCCESS) {
            action_log('删除', '删除广告，id：' . $data['id']);
            return [RESULT_SUCCESS, $result[1], $url];
        } else {
            return [RESULT_ERROR, $result[1]];
        }
    }
    
    /**
     * 设置广告状态
     */
    public function setAdvertisementStatus($data = [])
    {
        $url = url('advertisementList');
        
        if (empty($data['id'])) {
            return [RESULT_ERROR, '参数错误'];
        }
        
        $result = static::$commonAdvertisementLogic->toggleAdvertisementStatus($data);
        
        if ($result[0] == RESULT_SUCCESS) {
            action_log('状态', '切换广告状态，id：' . $data['id']);
            return [RESULT_SUCCESS, $result[1], $url];
        } else {
            return [RESULT_ERROR, $result[1]];
        }
    }
    
    /**
     * 设置广告排序
     */
    public function setAdvertisementSortOrder($data = [])
    {
        $url = url('advertisementList');
        
        if (empty($data['id']) || !isset($data['sort'])) {
            return [RESULT_ERROR, '参数错误'];
        }
        
        $updateData = [
            'id' => $data['id'],
            'sort' => intval($data['sort'])
        ];
        
        $result = static::$commonAdvertisementLogic->editAdvertisement($updateData);
        
        if ($result[0] == RESULT_SUCCESS) {
            action_log('排序', '设置广告排序，id：' . $data['id'] . '，排序：' . $data['sort']);
            return [RESULT_SUCCESS, '设置成功', $url];
        } else {
            return [RESULT_ERROR, $result[1]];
        }
    }
    
    /**
     * 获取广告统计数据
     */
    public function getAdvertisementStatistics($data = [])
    {
        $result = static::$commonAdvertisementLogic->getAdvertisementStatistics($data);
        
        return $result ?? [];
    }
    
    /**
     * 导出广告列表
     */
    public function exportAdvertisementList($where = [], $field = true, $order = 'a.sort DESC, a.id DESC')
    {
        $list = $this->getAdvertisementList($where, $field, $order, false);
        
        $titles = "广告标题,广告位置,链接地址,排序,状态,开始时间,结束时间,创建时间";
        $keys   = "title,position_text,link_url,sort,status_text,start_time,end_time,create_time";
        
        action_log('导出', '导出广告列表');
        
        export_excel($titles, $keys, $list, '广告列表');
    }
    
    /**
     * 获取广告位置选项
     */
    public function getPositionOptions()
    {
        return $this->modelAdvertisement->getPositionOptions();
    }
    
    /**
     * 获取广告状态选项
     */
    public function getStatusOptions()
    {
        return $this->modelAdvertisement->getStatusOptions();
    }
}